﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using EbbinghausWord.Common;
using EbbinghausWord.Entities;

namespace EbbinghausWord.DAL
{
    /// <summary>
    /// 测试、辅助类
    /// </summary>
    public static class DBTest
    {
        /// <summary>
        /// 生成词典数据库，正式数据
        /// </summary>
        public static void CreateSysDb()
        {
            //读取xml文件
            using (var db = new SysDataContext())
            {
                if (!db.DatabaseExists())
                {
                    var et = new ExecuteTimer(false);

                    db.CreateDatabase();
                    et.Show("CreateDatabase");

                    //创建词典表
                    CreateDictionaryTable(db);
                    et.Show("CreateDictionaryTable");

                    //创建书籍-单词关系表
                    CreateBookWordTable(db);
                    et.Show("CreateBookWordTable");

                    //创建用户单词表
                    CreateUserWord(db);
                    et.Show("CreateUserWord");
                }
            }
        }

        /// <summary>
        /// 创建书籍-单词关系表
        /// </summary>
        /// <param name="db"></param>
        private static void CreateBookWordTable(SysDataContext db)
        {
            //打开词典文件
            //var works = XElement.Load(@"words/relation.xml");

            //var stageIndex = 0;

            ////初始化词典
            //foreach (var element in works.Elements())
            //{
            //    db.TableBookWordEntities.InsertOnSubmit(TableBookWordEntity.FromXElemnt(element));

            //    //节约资源，每1000条记录保存一下
            //    if (++stageIndex < 1000) continue;

            //    db.SubmitChanges();
            //    stageIndex = 0;
            //}
            //db.SubmitChanges();
        }

        /// <summary>
        /// 创建词典表
        /// </summary>
        /// <param name="db"></param>
        private static void CreateDictionaryTable(SysDataContext db)
        {
            //var et = new ExecuteTimer(false);

            ////打开词典文件
            //var works = XElement.Load(@"words/dictionary.xml");

            //var stageIndex = 0;

            ////初始化词典
            //foreach (var element in works.Elements())
            //{
            //    db.TableDictionaryEntities.InsertOnSubmit(
            //        new TableDictionaryEntity()
            //            {
            //                English = element.Element("E").Value,

            //                CompleteData = element.ToString(),
            //            });
            //    //节约资源，每1000条记录保存一下
            //    //if (++stageIndex < 1000) continue;

            //    //db.SubmitChanges();
            //    //stageIndex = 0;
            //}
            //et.Show("Add");

            //db.SubmitChanges();
            //et.Show("Save");
        }

        /// <summary>
        /// 创建用户数据库
        /// </summary>
        public static void CreateUserWord(SysDataContext db)
        {
            //var now = DateTime.Now;

            ////打开系统库，词典表
            //var englishs = db.TableDictionaryEntities.Select(x => x.English).ToList();

            //foreach (var english in englishs)
            //{
            //    //数据写入到用户表中
            //    db.TableUserWordEntities.InsertOnSubmit(
            //        new TableUserWordEntity()
            //            {
            //                English = english,
            //                LastRemeberingDateTime = now,
            //                StartRemeberingDateTime = now,
            //                OrderRandom = Guid.NewGuid().ToString(),
            //            });
            //}

            //db.SubmitChanges();
        }
    }
}
